package com.zjht.gmcc.bean;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;


import com.zjht.gmcc.bean.action.OrderUtil;
import com.zjht.gmcc.bean.action.ProductWarnUtil;
import com.zjht.gmcc.bean.action.PublishUtil;

import com.zjht.gmcc.bean.util.InitParam;
import com.zjht.gmcc.bean.util.JDBCUtil;
import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyParam;
import com.zjht.gmcc.bean.util.MyPath;

public class ContextListener extends HttpServlet implements ServletContextListener {
	private static final long serialVersionUID = 5068735102506361000L;

	public ContextListener() {
	}

	private Timer timer = null;

	public void contextInitialized(ServletContextEvent event) {
		MyPath.REAL_PATH_CONTEXT_ROOT = event.getServletContext().getRealPath("/");
		MyPath.REAL_PATH_CONFIG = MyFormat.joinPath(MyPath.REAL_PATH_CONTEXT_ROOT, MyPath.PATH_CONFIG);
		MyPath.FILE_AJAX_SQL = MyFormat.joinPath(MyPath.REAL_PATH_CONFIG, "sql_ajax.conf");

		//Task.CTMSStartTime = Calendar.getInstance().getTime();// CTMS启动N分钟后，才能运行定时器程序

		System.out.println("系统启动，开始InitParam");
		JDBCUtil.getDataSource();// 初始化DataSource 
		BeginInitParam.Init();
		System.out.println("系统启动后InitParam结束");

		timer = new Timer(true);
		timer.schedule(new BeginInitParam(), MyCalendar(5, 20).getTime(), 86400000);// 每天凌晨运行
		

		// 备份并删除无用的消息
		timer.schedule(new myTimerBackupTask(true), MyCalendar(1, 20).getTime(), 86400000);
		timer.schedule(new myTimerBackupTask(true), MyCalendar(12, 30).getTime(), 86400000);

		// 备份全部消息(每两小时备份一次)
		timer.schedule(new myTimerBackupTask(false), MyCalendar(40).getTime(), 7200000);
	}

	public void contextDestroyed(ServletContextEvent event) {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
	}

	// 设定消息的启动时间
	private Calendar MyCalendar(int myhour, int myminute) {
		Calendar now = Calendar.getInstance();
		// 如果当前时间>预定时间myhour，则将预定时间推迟一天。
		if (now.get(Calendar.HOUR_OF_DAY) > myhour)
			now.add(Calendar.DAY_OF_MONTH, 1);
		now.set(Calendar.HOUR_OF_DAY, myhour);
		now.set(Calendar.MINUTE, myminute);
		return now;
	}

	private Calendar MyCalendar(int myminute) {
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MINUTE, myminute);
		return now;
	}
}

// 初始化参数（系统开始运行时和午夜运行)
class BeginInitParam extends java.util.TimerTask {

	public BeginInitParam() {
		System.out.println("InitParam初始化成功");
	}

	public void run() {
		System.out.println("InitParam再次初始化成功");
		Init();
	}

	/*
	 * 项目启动后初始化参数
	 */
	public static void Init() {
		try {
			InitParam.initMyParam();
			InitParam.initOrgaInfo();
			InitParam.initOperMenu();
			InitParam.initArea();
			InitParam.initParameter();
			InitParam.initDeptName();
			PublishUtil.ResetPublish();// 公告初始化
			InitParam.initPath();// 初始化系统使用的特殊路径
		    OrderUtil.initOrderStatus();//初始化合同状态
		    OrderUtil.initProOrderStatus();//初始化供应商合同状态
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("InitParam运行失败！");
		}
	}
}

/*
 * 备份消息
 */
class myTimerBackupTask extends java.util.TimerTask {
	private boolean _isDelTask = false;// 是否删除无用消息

	public myTimerBackupTask(boolean isDelTask) {
		_isDelTask = isDelTask;
		System.out.println("myTimerBackupTask(" + _isDelTask + ")初始化成功");
	}

	public void run() {
		try {
			
			System.out.println("myTimerBackupTask(" + _isDelTask + ")运行成功");
			if (!_isDelTask) {
				System.gc();
				System.out.println("ZSMS开始垃圾收集System.gc()");
				MyParam.arrSSO.clear();// 清空单点登录的验证码
				System.out.println("清空单点登录的验证码");
			}
		} catch (Exception e) {
			System.out.println("myTimerBackupTask(" + _isDelTask + ")运行失败！");
		}
	}
}

